package com.lam.code.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lam.code.base.BaseConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.util.List;
import java.util.Map;

/**
 * @description Web层辅助类
 * @author lam
 * @date 2019/10/6 23:44
 */
@Slf4j
public final class WebUtil {
    private WebUtil(){}

    public static <T> T getParameter(HttpServletRequest request, Class<T> cls) {
        return BeanUtil.mapToBean(getParameter(request),cls,true);
    }

    public static Map getParameter(HttpServletRequest request) {
        String body = getRequestBody(request);
        if (StrUtil.isNotEmpty(body)) {
            try {
                return JSON.parseObject(body, Map.class);
            } catch (Exception e) {
                try {
                    return XmlUtil.xmlToMap(body);
                } catch (Exception e1) {
                    log.error(ExceptionUtil.stacktraceToString(e));
                    return getRequestParam(body);
                }
            }
        }
        return getParameterMap(request);
    }

    /**
     * 获得参数Map
     *
     * @param request 请求对象
     * @return Map
     */
    public static Map<String, Object> getParameterMap(HttpServletRequest request) {
        return WebUtils.getParametersStartingWith(request, null);
    }

    public static Map<String, Object> getRequestParam(String param) {
        Map<String, Object> paramMap = MapUtil.newHashMap();
        if (null != param) {
            String[] params = param.split("&");
            for (String param2 : params) {
                String[] p = param2.split("=");
                if (p.length == 2) {
                    paramMap.put(p[0], p[1]);
                }
            }
        }
        return paramMap;
    }

    public static String getRequestBody(ServletRequest request) {
        String str, body = (String)request.getAttribute(BaseConstants.REQUEST_BODY);
        if (StrUtil.isEmpty(body)) {
            body = "";
            try {
                BufferedReader br = request.getReader();
                while ((str = br.readLine()) != null) {
                    body += str;
                }
                log.debug("request body===>{}", body);
                request.setAttribute(BaseConstants.REQUEST_BODY, body);
            } catch (Exception e) {
                return null;
            }
        }
        return body;
    }

    /**
     * Json字符串转List
     * @param jsonStr json字符串
     * @param clz 需转换的类型
     * @return T
     */
    public static <T> List<T> jsonToList(String jsonStr, Class<T> clz){
        Assert.notNull(clz,"argument clz is null");

        if (StringUtils.isNotEmpty(jsonStr)){
            return JSON.parseArray(jsonStr,clz);
        }
        return null;
    }

}
